package com.fkcai.hansoul.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fkcai.hansoul.model.dto.post.PostDraftDto;
import com.fkcai.hansoul.common.util.Result;
import com.fkcai.hansoul.model.dto.post.PostCreateDto;
import com.fkcai.hansoul.model.vo.post.GetPostVo;
import com.fkcai.hansoul.model.vo.post.PostBaseInfoVo;
import com.fkcai.hansoul.service.PostService;
import com.fkcai.hansoul.model.vo.post.PostFullInfoVo;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 帖子控制类：
 * 提供帖子相关的 RESTful API操作
 */
@RestController
@RequestMapping("/api/posts")
public class PostController {
	private static final Logger log = LoggerFactory.getLogger(PostController.class);
	@Autowired
	private PostService postService;
	
	
	/**
	 * 用户发帖接口
	 *
	 * @param postCreateDto 发帖信息
	 * @param images        图片列表
	 * @return 返回帖子信息
	 */
	@PostMapping("/createPost")
	public Result<PostBaseInfoVo> createPost (@Valid PostCreateDto postCreateDto,
	                                          @RequestPart(value = "images", required = false) List<MultipartFile> images
	) {
		PostBaseInfoVo data = postService.createPost(postCreateDto, images);
		
		return Result.success(data);
		
	}
	
	
	/**
	 * 创建草稿接口
	 *
	 * @param postDraftDto 草稿信息
	 * @param images       图片列表
	 * @return 草稿信息
	 */
	@PostMapping("/saveDraft")
	public Result<PostBaseInfoVo> createPostDraft (@Valid PostDraftDto postDraftDto,
	                                               @RequestPart(value = "images", required = false) List<MultipartFile> images
	) {
		PostBaseInfoVo data = postService.createPostDraft(postDraftDto, images);
		
		return Result.success(data);
	}
	
	
	/**
	 * 获取用户帖子接口
	 *
	 * @param userId 用户 id
	 * @return 帖子列表
	 */
	@GetMapping("/getPosts/{userId}")
	public Result<List<GetPostVo>> getPosts (@PathVariable Integer userId) {
		
		List<GetPostVo> posts = postService.getPostsByUserId(userId);
		
		return Result.success(posts);
	}
	
	/**
	 * 根据帖子 id 获取帖子详情接口
	 *
	 * @param postId 帖子 id
	 * @return 帖子详情
	 */
	@GetMapping("/exclude/getPostFullInfo/{postId}")
	public Result<PostFullInfoVo> getPostInfo (@PathVariable Integer postId) {
		
		PostFullInfoVo post = postService.getPostByPostId(postId);
		
		return Result.success(post);
	}
	
	/**
	 * 根据分类 id 获取帖子列表接口
	 * @param categoryId 分类 id
	 * @param page 页码
	 * @param size 每页数量
	 * @return 帖子列表
	 */
	@GetMapping("/exclude/getPostsByCategoryId/{categoryId}")
	public Result<Page<GetPostVo>> getPostsByCategoryId (
			@PathVariable Integer categoryId,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "size", defaultValue = "20") int size
	) {
		// 校验页码和每页数量
		page = Math.max(page, 1);
		size = Math.min(Math.max(size, 1), 20);
		
		int offset = (page - 1) * size;
		
		List<GetPostVo> posts = postService.getPostsByCategoryId(categoryId, offset, size);
		
		// 获取总数
		long total = postService.countPosts(categoryId);
		
		// 创建 MyBatis-Plus Page 对象
		Page<GetPostVo> pageResult = new Page<>(page, size, total);
		pageResult.setRecords(posts);
		
		return Result.success(pageResult);
	}
	
	@GetMapping("/exclude/getAllPosts")
	public Result<Page<GetPostVo>> getAllPosts (
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "size", defaultValue = "20") int size
	) {
		// 校验页码和每页数量
		page = Math.max(page, 1);
		size = Math.min(Math.max(size, 1), 20);
		
		int offset = (page - 1) * size;
		
		List<GetPostVo> posts = postService.getAllPosts(offset, size);
		
		// 获取总数
		long total = postService.countPosts(null);
		
		// 创建 MyBatis-Plus Page 对象
		Page<GetPostVo> pageResult = new Page<>(page, size, total);
		pageResult.setRecords(posts);
		
		return Result.success(pageResult);
	
	}
	
}
